package com.shou.domain.user.model.valobj;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

@AllArgsConstructor
public enum GenderEnum {
    MALE(1,"男"),
    FEMALE(0,"女");

    private final Integer code;
    private final String name;

    /**
     * 根据code获取枚举,一般在从数据库中获取到code后，根据code获取枚举
     * @param code 0/1
     * @return 枚举
     */
    public static GenderEnum getGenderEnumByCode(Integer code) {
        for (GenderEnum value : values()) {
            if (value.code.equals(code)) {
                return value;
            }
        }
        return null;
    }

    /**
     * 根据枚举获取code,一般在将枚举存入数据库时，根据枚举获取code
     * @param genderEnum 枚举
     * @return 0/1
     */
    public static Integer getCode(GenderEnum genderEnum) {
        if(Objects.isNull(genderEnum)){
            return null;
        }
        for (GenderEnum value : values()) {
            if (value.equals(genderEnum)) {
                return value.code;
            }
        }
        return null;
    }
}
